package leetcode.medium.limit50;

/**
 * Z字形变换
 *
 * @author yaoyao1.liu@zhaopin.com.cn
 * @since 2022-06-29 16:31
 **/
public class No6 {
    public static void main(String[] args) {
        long currentTimeMillis = System.currentTimeMillis();
        String s = "cbxdwjccgtdoqiscyspqzvuqivzptlpvooynyapgvswoaosaghrffnxnjyeeltzaizniccozwknwyhzgpqlwfkjqipuujvwtxlbznryjdohbvghmyuiggtyqjtmuqinntqmihntkddnalwnmsxsatqqeldacnnpjfermrnyuqnwbjjpdjhdeavknykpoxhxclqqedqavdwzoiorrwwxyrhlsrdgqkduvtmzzczufvtvfioygkvedervvudneghbctcbxdxezrzgbpfhzanffeccbgqfmzjqtlrsppxqiywjobspefujlxnmddurddiyobqfspvcoulcvdrzkmkwlyiqdchghrgytzdnobqcvdeqjystmepxcaniewqmoxkjwpymqorluxedvywhcoghotpusfgiestckrpaigocfufbubiyrrffmwaeeimidfnnzcphkflpbqsvtdwludsgaungfzoihbxifoprwcjzsdxngtacw";
        int numRows = 479;
        No6 no6 = new No6();
        System.out.println(no6.convert(s, numRows));
        System.out.println(System.currentTimeMillis() - currentTimeMillis);

    }

    public String convertBak(String s, int numRows) {
        String[] strings = s.split("");
        if (numRows == 1) {
            return s;
        }
        String[][] stringsList = new String[numRows][strings.length];
        int index = 0;
        int column = 0;
        while (index < strings.length) {
            for (int i = 0; i < numRows; i++) {
                if (index > strings.length - 1) {
                    break;
                }
                stringsList[i][column] = strings[index];
                index++;
            }
            column++;
            for (int rows = numRows - 2; rows > 0; rows--) {
                if (index > strings.length - 1) {
                    break;
                }
                stringsList[rows][column++] = strings[index++];
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String[] strings1 : stringsList) {
            for (String s1 : strings1) {
                if (s1 == null) {
                    System.out.print(" ");
                    continue;
                }
                System.out.print(s1 + " ");
                stringBuilder.append(s1);
            }
            System.out.println();
        }
        return stringBuilder.toString();
    }

    public String convert(String s, int numRows) {
        int n = s.length();
        int r = numRows;
        if (r == 1 || r >= n) {
            return s;
        }

        int t = numRows + numRows - 2;
        // n = 10 r = 3
        // t = 4 一个周期4个字符
        // 每个周期会占用 1+r-2=r-1 列
        int c = (n + t - 1) / t * (r - 1);
        char[][] mat = new char[r][c];
        for (int i = 0, x = 0, y = 0; i < n; ++i) {
            mat[x][y] = s.charAt(i);
            if (i % t < r - 1) {
                ++x;
            } else {
                --x;
                ++y;
            }
        }
        StringBuilder ans = new StringBuilder();
        for (char[] row : mat) {
            for (char ch : row) {
                if (ch != 0) {
                    ans.append(ch);
                }
            }
        }
        return ans.toString();
    }
}
